package conMgr.prog;

import java.util.LinkedList;
import java.util.List;

/**
 * The base class for an object that reports errors to ErrorListeners.
 * @author is4@uw.edu
 */
public abstract class ErrorProvider
{
	/**
	 * A list of registered error listeners.
	 */
	private final List<ErrorListener> errorListeners;
	
	/**
	 * Initializes a new data store base.
	 */
	protected ErrorProvider()
	{
		errorListeners = new LinkedList<ErrorListener>();
	}
	
	/**
	 * Registers a new error listener.
	 * @param errorListener The error listener to register.
	 */
	public void addErrorListener(final ErrorListener errorListener)
	{
		if (errorListener != null)
			errorListeners.add(errorListener);
	}
	
	/**
	 * Notifies listeners that an error has occurred.
	 * @param description The description of the error.
	 */
	protected void error(final String description)
	{
		error(description, null);
	}
	
	/**
	 * Notifies listeners that an error has occurred.
	 * @param description The description of the error.
	 * @param e The exception associated with this error, if any.
	 */
	protected void error(final String description, final Exception e)
	{
		for (ErrorListener errorListener : errorListeners)
			errorListener.errorOccurred(description, e);
	}
}
